###############################################################################
# 
#  Copyright (c) 2005, 2006, 2007, 2008, 2009
#  University of Rochester
#  Department of Computer Science
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
#     * Neither the name of the University of Rochester nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
# 
# 
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.

# only the path to config.h should be specified
PATH_TO_STM_INSTALL = ..

# pull in all global Make variable definitions
include $(PATH_TO_STM_INSTALL)/Makefile.inc

# set build flags
CXXFLAGS += -I$(PATH_TO_STM_INSTALL)/

# specify paths.  everything goes into OBJDIR.
# treat lib, cm, and support source files differently
OBJDIR       = ./obj
LIB_SOURCES  = rstm
CM_SOURCES   = ContentionManager
SUPT_SOURCES = GCHeap
LIB_CFILES   = $(patsubst %, %.cpp, $(LIB_SOURCES))
CM_CFILES    = $(patsubst %, cm/%.cpp, $(CM_SOURCES))
SUPT_CFILES  = $(patsubst %, support/%.cpp, $(SUPT_SOURCES))
LIB_OFILES   = $(patsubst %, $(OBJDIR)/%.o, $(LIB_SOURCES))
CM_OFILES    = $(patsubst %, $(OBJDIR)/%.o, $(CM_SOURCES))
SUPT_OFILES  = $(patsubst %, $(OBJDIR)/%.o, $(SUPT_SOURCES))
GOAL         = $(OBJDIR)/libstm.a
PREGOAL      = $(OBJDIR)/lib$(STM_VERSION).a
DEPEND       = Makefile.depend

# build rules
.PHONY: all clean realclean

all: $(DEPEND) $(OBJDIR) $(GOAL)
	@echo $(GOAL) complete

$(OBJDIR):
	mkdir -p $@

# /clean/ gets rid of the configuration specified by STM_VERSION
clean:
	@rm -f $(GOAL) $(OBJDIR)/$(STM_VERSION).o $(PREGOAL)

# /realclean/ gets rid of everything you've built, all configurations
realclean:
	@rm -rf obj/ $(DEPEND)

# since we hacked the paths from the dependency builder, we need an explicit
# compilation rule
$(LIB_OFILES) $(CM_OFILES) $(SUPT_OFILES):
	$(CXX) $< $(CXXFLAGS) -c -o $@

# there exist configurations for which CM_OFILES and SUPT_OFILES are not
# needed, but it's more hassle than it's worth to exclude them
$(GOAL): $(PREGOAL)
	cp $< $@

$(PREGOAL): $(OBJDIR)/$(STM_VERSION).o $(CM_OFILES) $(SUPT_OFILES)
	$(AR) cru $@ $^
# dependency builder uses sed to put the .o files in OBJDIR
# $(DEPEND): $(LIB_CFILES) $(CM_CFILES) $(SUPT_CFILES) ../config.h
$(DEPEND):
	@echo -n "Building dependencies as $@..."
	@echo '# DO NOT EDIT THIS FILE -- it is automatically generated' > $@
	@$(CXX) -MM $(CXXFLAGS) $(LIB_CFILES) $(CM_CFILES) $(SUPT_CFILES) >> $@
	@sed '/.o:/ s/^/$$(OBJDIR)\//' $(DEPEND) > $(DEPEND).tmp; mv $(DEPEND).tmp $(DEPEND)
	@echo "done"

-include $(DEPEND)
